import ProTable from '@ant-design/pro-table';
import React, {useState} from 'react';
import {defaultPagination, paginationTool} from '@/utils/util';
import {useMount, useRequest} from 'ahooks';
import {Button, Space} from 'antd';
import {getByMiniSerachList} from '@/services/ant-design-pro/agent';
import {Link} from "@umijs/preset-dumi/lib/theme";
import {findByWalletBackLogList, findByWalletBackLogList1} from "@/services/ant-design-pro/log";
import FormModalBtn from "@/components/FormModalBtn";
import {editIntegralReChange, editWalletReChange} from "@/services/ant-design-pro/users";
import {getResult} from "@/services/ant-design-pro/attribute";

const getAttr = async (key: string) => {
  return await getResult ( key, 1 );
};
const columns = [
  {
    title: '编号',
    dataIndex: 'id',
    search: false,
    key: 'id',
  },

  {
    title: '头像',
    dataIndex: 'avatar',
    valueType: 'avatar',
    key: 'avatar',
    search: false,
  },
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => {
      return (
        <Link target="_blank" to={'/users/tree?userId=' + row?.id}>
          {_}
        </Link>
      );
    },
  },

  {
    title: '手机号',
    dataIndex: 'phone',
    key: 'phone',
  },
  {
    title: '邀请人',
    dataIndex: 'parentName',
    key: 'parentName',
  },
  {
    title: '代理等级',
    dataIndex: 'agentName',
    search: false,
    key: 'agentName',
  },
  {
    title: '代理等级',
    dataIndex: 'agentId',
    key: 'agentId',
    valueEnum: await getByMiniSerachList ( 0 ),
    valueType: 'select',
    hideInTable: true,
  },
  {
    title: "余额",
    key: "balance",
    dataIndex: "balance",
    valueType: "money",
    search: false,
  },  {
    title: "积分",
    key: "integral",
    dataIndex: "integral",
    search: false,
  },

  {
    title: '时间',
    dataIndex: 'createDate',
    valueType: 'dateTime',
    hideInSearch: true,
    key: 'createDate',
  }
];
//
const cloneTbale = () => {
  const [pagination, setPagination] = useState<API.Pagination> ( defaultPagination () );
  const {
    run: fetchByPageList,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByWalletBackLogList1, {
    debounceWait: 100,
    manual: true,
  } );

  useMount ( () => {
    columns[14] = {
      title: '操作',
      width: '164px',
      key: 'option',
      valueType: 'option',
      render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => [
        <FormModalBtn initialValues={{name: row?.name, userId: row?.id}} title="财务" btnText="财务" columns={[{
          title: "userId", dataIndex: 'userId',
          key: 'userId',
          width: 'md',
          readonly: true,
          formItemProps: {
            hidden: true
          }

        }, {
          title: "姓名", dataIndex: 'name',
          key: 'name',
          width: 'md',
          readonly: true,

        }, {
          title: "财务动作", dataIndex: 'state',
          key: 'state',
          width: 'md',
          valueType: "select",
          request: () => {
            return getAttr ( "usersblancewithdrawaltype" );
          }, formItemProps: {
            debounceTime: 300,
            rules: [
              {
                required: true
              }
            ]
          }
        }, {
          title: "金额", dataIndex: 'price',
          key: 'price',
          width: 'md',
          valueType: "money",
          fieldProps: {
            help: "gggggggggggggggggggggggg"
          }, formItemProps: {
            rules: [
              {
                required: true
              }
            ]
          }


        }, {
          title: "处理类型", dataIndex: 'type',
          key: 'type',
          width: 'md',
          valueType: "select",
          request: () => {
            return getResult ( "userswithdrawaltype", 1 );
          }, formItemProps: {
            debounceTime: 300,
            rules: [
              {
                required: true
              }
            ]
          }
        }, {
          title: "备注", dataIndex: 'remark',
          key: 'remark',
          width: 'md',
          valueType: "textarea", formItemProps: {
            rules: [
              {
                required: true
              }
            ]
          }
        }]} request={editWalletReChange}  onChange={()=>{
          refresh()
        }}/>,
        <FormModalBtn initialValues={{name: row?.name, userId: row?.id}} title="积分" btnText="积分" columns={[{
          title: "userId", dataIndex: 'userId',
          key: 'userId',
          width: 'md',
          readonly: true,
          formItemProps: {
            hidden: true
          }

        }, {
          title: "姓名", dataIndex: 'name',
          key: 'name',
          width: 'md',
          readonly: true,

        }, {
          title: "积分动作", dataIndex: 'state',
          key: 'state',
          width: 'md',
          valueType: "select",

          request: () => {
            return getAttr ( "usersblancewithdrawaltype" );
          }, formItemProps: {
            debounceTime: 300,
            rules: [
              {
                required: true
              }
            ]
          }
        }, {
          title: "积分", dataIndex: 'integral',
          key: 'integral',
          width: 'md',
          valueType: "number",
          formItemProps: {
            rules: [
              {
                required: true
              }
            ]
          }


        }, {
          title: "备注", dataIndex: 'remark',
          key: 'remark',
          width: 'md',
          valueType: "textarea", formItemProps: {
            rules: [
              {
                required: true
              }
            ]
          }
        }]} request={editIntegralReChange} onChange={()=>{
          refresh()
        }}/>

      ],
    }

    fetchByPageList ();
  } );

  return (
    <ProTable
      search={false}
      dataSource={dataList?.data?.list}
      loading={loading}
      pagination={paginationTool ( pagination, dataList?.data?.total, (data1: API.Pagination) => {
        setPagination ( data1 );

        fetchByPageList ( {...data1} );
      } )}
      search={{
        defaultCollapsed: false,
        labelWidth: 'auto',
      }}
      onSubmit={(params) => {
        fetchByPageList ( params );
        console.log ( 'onSubmit', params );
      }}
      cardBordered={true}
      columns={columns}
    ></ProTable>
  );
};

export default cloneTbale;
